package com.aimashi.railwaytoolmanagesystem.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;

import com.aimashi.railwaytoolmanagesystem.R;
import com.aimashi.railwaytoolmanagesystem.activity.history.Hostory;
import com.aimashi.railwaytoolmanagesystem.activity.scantool.ScanTool;
import com.aimashi.railwaytoolmanagesystem.activity.sendcard.SendCard;
import com.aimashi.railwaytoolmanagesystem.activity.setting.Setting;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

@SuppressLint("InflateParams")
public class Main extends Base {

	
	@ViewInject(R.id.main_menu_radiogroup)
	private RadioGroup navigationRadioGroupFirst;
	
	
	@ViewInject(R.id.setting_viewPager)
	private ViewPager mViewPager;
	
	
	@ViewInject(R.id.sendcard_read_card)
	private Button btnRead;
	

	private SendCard sendCardPage;
	private Hostory hostoryPage;
	private ScanTool scanToolPage;
	private Setting settingPage;
	
	private List<Fragment> listViews; // viewPage页面列表


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ViewUtils.inject(this); // 注入view和事件
		
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		sendCardPage = new SendCard();
		hostoryPage = new Hostory();
		scanToolPage = new ScanTool();
		settingPage = new Setting();
		
		listViews = new ArrayList<Fragment>();
		listViews.add(scanToolPage);
		listViews.add(hostoryPage);
		listViews.add(sendCardPage);
		listViews.add(settingPage);
		
		mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager(),listViews));
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) { //此方法是页面跳转完后得到调用
				//同步选中导航栏对应的选项
				navigationRadioGroupFirst.check(navigationRadioGroupFirst.getChildAt(arg0).getId());
				if(arg0 == 1){
					hostoryPage.InitModel();
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) { //   页面滑动的时候调用此方法 , 利用这个方法，可以做其他控件的同步滑动
				
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {//此方法在状态改变时调用，共有三个状态：0，什么都没做；1，正在滑动；2，滑动完毕

			}
		});
		
		//选中导航栏第一项
		navigationRadioGroupFirst.check(navigationRadioGroupFirst.getChildAt(0).getId());
		
		/**
		 * 用一个定时器控制当打开这个Activity的时候就出现软键盘
		 */
//		Timer timer = new Timer();
//		timer.schedule(new TimerTask() {
//			@Override
//			public void run() {
//				ShowOrHidekeyBord();
//			}
//		}, 1500);
	}

	@OnClick({ R.id.first_radiobutton, R.id.second_radiobutton,R.id.three_radiobutton,R.id.four_radiobutton})
	public void searchOnclick(View v) {
		
		switch (v.getId()) {
		case R.id.first_radiobutton:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.second_radiobutton:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.three_radiobutton:
			mViewPager.setCurrentItem(2);
			break;
		case R.id.four_radiobutton:
			mViewPager.setCurrentItem(3);
			break;
		default:
			break;
		}
	}
}
